package chapter6;

/**
 * 重写equals方法，自定义相等的规则，重写equals方法应该满足以下条件
 * 1.自反性：对任意x, x.equals(x)一定返回true
 * 2.对称性：对任意x和y，x.equals(y)返回true，则y.equals(x)也返回true
 * 3.传递性: 对任意x,y,z，如果x.equals(y)为true，y.equals(z)为true,则x.equals(z)也为true
 * 4.一致性：对任意x和y，如果对象中的比较条件未发生改变，无论比较多少次，比较结果不应该有变化
 * 5.任何不是null的x，x.equals(null)一定返回false
 */
public class Person {
    private String name;
    private String idStr;

    public Person(String name, String idStr) {
        this.name = name;
        this.idStr = idStr;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdStr() {
        return idStr;
    }

    public void setIdStr(String idStr) {
        this.idStr = idStr;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        if(obj != null && obj.getClass() == Person.class) {
            Person personObj = (Person) obj;
            if(personObj.getIdStr().equals(this.getIdStr())) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Person person1 = new Person("leemz", "123");
        Person person2 = new Person("cwh", "123");
        Person person3 = new Person("zhangsan", "456");
        System.out.println(person1.equals(person2));
        System.out.println(person2.equals(person3));
    }
}
